package com.atguigu.srb.core.controller.admin;

import com.atguigu.common.exception.Assert;
import com.atguigu.common.result.ResponseEnum;
import com.atguigu.srb.core.pojo.entity.IntegralGrade;
import com.atguigu.srb.core.service.IntegralGradeService;
import com.atguigu.common.result.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * @Author: czl
 * @Date: 2022-04-06 19:28
 * @Description: com.example.srb.core.controller.admin
 * @version: 1.0
 */
@Api(tags = "积分管理")
@RestController
@RequestMapping("/admin/core/integralGrade")
@Slf4j
public class AdminIntegralGradeController {

    @Resource
    private IntegralGradeService integralGradeService;

    @ApiOperation("积分等级列表")
    @GetMapping("/list")
    public R listAll() {

        List<IntegralGrade> list = integralGradeService.list();
        return R.ok().data("list", list).message("查询成功");
    }

    @ApiOperation(value = "根据id删除积分", notes = "逻辑删除")
    @DeleteMapping("/remove/{id}")
    public R removeById(@ApiParam(value = "数据id", required = true, example = "1") @PathVariable Long id) {
        boolean result = integralGradeService.removeById(id);
        if (result) {
            return R.ok().message("删除成功");
        } else {
            return R.ok().message("删除失败");
        }

    }

    @ApiOperation("新增积分等级")
    @PostMapping("/save")
    public R insertIntegralGrade(@ApiParam(value = "积分等级对象", required = true)
                                 @RequestBody IntegralGrade integralGrade) {
        Assert.notNull(integralGrade.getBorrowAmount(), ResponseEnum.BORROW_AMOUNT_NULL_ERROR);
        boolean result = integralGradeService.save(integralGrade);
        if (result) {
            return R.ok().message("添加成功");
        } else {
            return R.ok().message("添加失败");
        }
    }

    @ApiOperation("查询积分等级根据ID")
    @GetMapping("/getById/{id}")
    public R getById(@ApiParam(value = "积分等级id", required = true) @PathVariable Long id) {
        IntegralGrade integralGrade = integralGradeService.getById(id);
        return R.ok().data("integralGrade", integralGrade);
    }

    @ApiOperation("根据id修改积分等级")
    @PutMapping("/updateById")
    public R updateById(@ApiParam(value = "积分等级对象", required = true)
                        @RequestBody IntegralGrade integralGrade) {
        boolean result = integralGradeService.updateById(integralGrade);
        if (result) {
            return R.ok().message("修改成功");
        } else {
            return R.error().message("修改失败");
        }
    }

}
